import 'package:flutter/material.dart';
import 'package:habit_master/core/models/habit.dart';
import 'package:fl_chart/fl_chart.dart';

class StatisticsScreen extends StatefulWidget {
  const StatisticsScreen({Key? key}) : super(key: key);

  @override
  _StatisticsScreenState createState() => _StatisticsScreenState();
}

class _StatisticsScreenState extends State<StatisticsScreen> {
  int _selectedTabIndex = 0;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('统计分析'),
        elevation: 0,
      ),
      body: Column(
        children: [
          _buildTabs(),
          Expanded(
            child: _buildTabContent(),
          ),
        ],
      ),
    );
  }
  
  Widget _buildTabs() {
    return Container(
      height: 50,
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 5,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        children: [
          _buildTabItem(0, '总览'),
          _buildTabItem(1, '周'),
          _buildTabItem(2, '月'),
          _buildTabItem(3, '年'),
        ],
      ),
    );
  }
  
  Widget _buildTabItem(int index, String title) {
    final isSelected = _selectedTabIndex == index;
    
    return Expanded(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _selectedTabIndex = index;
          });
        },
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 3,
                color: isSelected
                    ? Theme.of(context).primaryColor
                    : Colors.transparent,
              ),
            ),
          ),
          child: Text(
            title,
            style: TextStyle(
              color: isSelected
                  ? Theme.of(context).primaryColor
                  : Colors.grey,
              fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ),
      ),
    );
  }
  
  Widget _buildTabContent() {
    switch (_selectedTabIndex) {
      case 0:
        return _buildOverviewTab();
      case 1:
        return _buildWeeklyTab();
      case 2:
        return _buildMonthlyTab();
      case 3:
        return _buildYearlyTab();
      default:
        return _buildOverviewTab();
    }
  }
  
  Widget _buildOverviewTab() {
    return SingleChildScrollView(
      padding: const EdgeInsets.all(15),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildSummaryCards(),
          const SizedBox(height: 20),
          _buildCompletionChart(),
          const SizedBox(height: 20),
          _buildBestStreakCard(),
          const SizedBox(height: 20),
          _buildHabitPerformance(),
        ],
      ),
    );
  }
  
  Widget _buildSummaryCards() {
    return GridView.count(
      crossAxisCount: 2,
      crossAxisSpacing: 15,
      mainAxisSpacing: 15,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        _buildSummaryCard(
          '85%',
          '习惯完成率',
          Icons.check_circle,
        ),
        _buildSummaryCard(
          '31',
          '最长连续天数',
          Icons.calendar_today,
        ),
        _buildSummaryCard(
          '15',
          '当前连续天数',
          Icons.local_fire_department,
        ),
        _buildSummaryCard(
          '132',
          '总打卡次数',
          Icons.assignment_turned_in,
        ),
      ],
    );
  }
  
  Widget _buildSummaryCard(String value, String label, IconData icon) {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            icon,
            color: Theme.of(context).primaryColor,
            size: 28,
          ),
          const SizedBox(height: 10),
          Text(
            value,
            style: const TextStyle(
              fontSize: 24,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 5),
          Text(
            label,
            style: const TextStyle(
              color: Colors.grey,
              fontSize: 12,
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }
  
  Widget _buildCompletionChart() {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '最近完成情况',
                style: Theme.of(context).textTheme.headline6,
              ),
              const Text(
                '最近7天',
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 12,
                ),
              ),
            ],
          ),
          const SizedBox(height: 20),
          SizedBox(
            height: 200,
            child: BarChart(
              BarChartData(
                alignment: BarChartAlignment.spaceAround,
                maxY: 100,
                barTouchData: BarTouchData(
                  enabled: false,
                ),
                titlesData: FlTitlesData(
                  show: true,
                  bottomTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: true,
                      getTitlesWidget: (double value, TitleMeta meta) {
                        String text;
                        switch (value.toInt()) {
                          case 0:
                            text = '一';
                            break;
                          case 1:
                            text = '二';
                            break;
                          case 2:
                            text = '三';
                            break;
                          case 3:
                            text = '四';
                            break;
                          case 4:
                            text = '五';
                            break;
                          case 5:
                            text = '六';
                            break;
                          case 6:
                            text = '日';
                            break;
                          default:
                            text = '';
                            break;
                        }
                        return Text(
                          text,
                          style: const TextStyle(
                            color: Colors.grey,
                            fontSize: 12,
                          ),
                        );
                      },
                      reservedSize: 30,
                    ),
                  ),
                  leftTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: false,
                    ),
                  ),
                  topTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: false,
                    ),
                  ),
                  rightTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: false,
                    ),
                  ),
                ),
                borderData: FlBorderData(
                  show: false,
                ),
                barGroups: [
                  _buildBarGroup(0, 80),
                  _buildBarGroup(1, 100),
                  _buildBarGroup(2, 60),
                  _buildBarGroup(3, 90),
                  _buildBarGroup(4, 100),
                  _buildBarGroup(5, 70),
                  _buildBarGroup(6, 50),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
  
  BarChartGroupData _buildBarGroup(int x, double y) {
    return BarChartGroupData(
      x: x,
      barRods: [
        BarChartRodData(
          toY: y,
          color: Theme.of(context).primaryColor,
          width: 20,
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(4),
            topRight: Radius.circular(4),
          ),
        ),
      ],
    );
  }
  
  Widget _buildBestStreakCard() {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Row(
        children: [
          Container(
            width: 50,
            height: 50,
            decoration: BoxDecoration(
              color: const Color(0xFFFFF9C4),
              borderRadius: BorderRadius.circular(25),
            ),
            child: const Icon(
              Icons.local_fire_department,
              color: Color(0xFFFFA000),
              size: 30,
            ),
          ),
          const SizedBox(width: 15),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: const [
                Text(
                  '晨跑5公里',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
                SizedBox(height: 5),
                Text(
                  '你的最佳习惯',
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 13,
                  ),
                ),
              ],
            ),
          ),
          const Text(
            '31',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 24,
              color: Color(0xFFFFA000),
            ),
          ),
        ],
      ),
    );
  }
  
  Widget _buildHabitPerformance() {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '习惯完成情况',
            style: Theme.of(context).textTheme.headline6,
          ),
          const SizedBox(height: 15),
          _buildPerformanceItem(
            '晨跑5公里',
            31,
            31,
            Icons.directions_run,
          ),
          const Divider(),
          _buildPerformanceItem(
            '阅读30分钟',
            25,
            31,
            Icons.book,
          ),
          const Divider(),
          _buildPerformanceItem(
            '喝水2000ml',
            28,
            31,
            Icons.local_drink,
          ),
          const Divider(),
          _buildPerformanceItem(
            '22:30前睡觉',
            22,
            31,
            Icons.nightlight_round,
          ),
        ],
      ),
    );
  }
  
  Widget _buildPerformanceItem(
    String title, 
    int completed, 
    int total, 
    IconData icon,
  ) {
    final percentage = (completed / total * 100).toInt();
    
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: const Color(0xFFE3F2FD),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Icon(
              icon,
              color: Theme.of(context).primaryColor,
              size: 20,
            ),
          ),
          const SizedBox(width: 15),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  '$completed/$total 天',
                  style: const TextStyle(
                    fontSize: 13,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ),
          Text(
            '$percentage%',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Theme.of(context).primaryColor,
              fontSize: 16,
            ),
          ),
        ],
      ),
    );
  }
  
  Widget _buildWeeklyTab() {
    // 这里实现周视图的内容
    return const Center(
      child: Text('周视图'),
    );
  }
  
  Widget _buildMonthlyTab() {
    // 这里实现月视图的内容
    return const Center(
      child: Text('月视图'),
    );
  }
  
  Widget _buildYearlyTab() {
    // 这里实现年视图的内容
    return const Center(
      child: Text('年视图'),
    );
  }
}
